package com.future.servlet;

import com.future.biz.LocalSurveyBIZ;
import com.future.dao.AreaDAO;
import com.future.model.bo.LocalSurveyBO;
import com.future.model.bo.UserBO;
import com.future.model.po.FirstFillBlankModel;
import com.future.model.po.InforRateModel;
import com.future.model.po.ServicesAnswerTotleModel;
import com.future.model.po.ShareTotleModel;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子政务发展情况调查
 * 第一部分 地方调查（省市县三级地方）
 * @author petter   Jan 20, 2014
 *
 */
public class LocalSurveyServlet extends HttpServlet {

	public LocalSurveyServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(	HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		String flag = request.getParameter("flag");
		if (flag != null && !("").equals(flag)) {
			int iflag = Integer.valueOf(flag);
			switch (iflag) {
			case 1:
				showLocalSurvey(request, response);
				break;
			case 2:
				updateLocalSurvey(request, response);
				break;
			}
		}
	}

	/**
	 * 显示第一部分 地方调查（省市县三级地方）
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void showLocalSurvey(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		LocalSurveyBIZ localSurveyBiz = new LocalSurveyBIZ();
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		LocalSurveyBO localSurveyBo = localSurveyBiz.generatSurveyByUser(userBo);

		request.setAttribute("localSurveyBo", localSurveyBo);
		request.getRequestDispatcher("/local_survey.jsp").forward(request, response);
		return;
	}
	/**
	 * 修改第一部分 地方调查（省市县三级地方）
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void updateLocalSurvey(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		FirstFillBlankModel firstFillBlankModel = this.getFirstFillBlankModel(request, response);
		List<InforRateModel> inforRateModels = this.getInforRateModel(request, response);
		List<ShareTotleModel> shareTotleModels = this.getShareTotleModel(request, response);
		List<ServicesAnswerTotleModel> servicesAnswerTotleModels = this.getServicesAnswerTotleModels(request, response);
		
		
		LocalSurveyBIZ localSurveyBiz = new LocalSurveyBIZ();
		boolean param = localSurveyBiz.updateLocalSurvey(firstFillBlankModel,inforRateModels,shareTotleModels,servicesAnswerTotleModels);
		AreaDAO areaDao = new AreaDAO();
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		areaDao.updateAreaByCode(userBo.getUser().getUsername());
		request.setAttribute("param", param);
		request.getRequestDispatcher("/com/future/servlet/LocalSurveyServlet?flag=1").forward(request, response);
		return;
	}
	
	private List<InforRateModel> getInforRateModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<InforRateModel> inforRateModels = new ArrayList<InforRateModel>();
		InforRateModel inforRateModel = null;
		String[] departmentIds = request.getParameterValues("departmentId");
		String[] rates = request.getParameterValues("rate")!=null?request.getParameterValues("rate"):null;
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		if (arrayIsNotBank(departmentIds)) {
			for (int i = 0; i < departmentIds.length; i++) {
				inforRateModel = new InforRateModel();
				inforRateModel.setDepartmentId(isNotbank(departmentIds[i])?Integer.valueOf(departmentIds[i]):null);
				inforRateModel.setRate(isNotbank(rates[i])?Float.valueOf(rates[i]):null);
				inforRateModel.setUserid(userBo.getUser().getId());
				inforRateModels.add(inforRateModel);
			}
		}
		return inforRateModels;
	}
	private List<ShareTotleModel> getShareTotleModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<ShareTotleModel> shareTotleModels = new ArrayList<ShareTotleModel>();
		ShareTotleModel shareTotleModel = null;
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		String[] servicesIds = request.getParameterValues("servicesId");
		String[] counts = request.getParameterValues("count");
		if (arrayIsNotBank(servicesIds)) {
			for (int i = 0; i < servicesIds.length; i++) {
				shareTotleModel = new ShareTotleModel();
				shareTotleModel.setUserId(userBo.getUser().getId());
				shareTotleModel.setId(isNotbank(servicesIds[i])?Integer.valueOf(servicesIds[i]):null);
				shareTotleModel.setTotle(isNotbank(counts[i])?Integer.valueOf(counts[i]):null);
				shareTotleModels.add(shareTotleModel);
			}
		}
		return shareTotleModels;
	}
	private List<ServicesAnswerTotleModel> getServicesAnswerTotleModels(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String[] ids = request.getParameterValues("id");
		String[] towns = request.getParameterValues("town");
	    String[] communitys = request.getParameterValues("community");
	    List<ServicesAnswerTotleModel> lists = new ArrayList<ServicesAnswerTotleModel>();
	    ServicesAnswerTotleModel model = null;
	    if(arrayIsNotBank(ids)){
	    	for (int i = 0; i < ids.length; i++) {
	    		model = new ServicesAnswerTotleModel();
		    	model.setId(isNotbank(ids[0])?Integer.valueOf(ids[0]):null);
		    	model.setCommunity(isNotbank(communitys[0])?Integer.valueOf(communitys[0]):null);
		    	model.setTown(isNotbank(towns[0])?Integer.valueOf(towns[0]):null);
		    	lists.add(model);
			}
	    }
		return lists;
	}
	
	private FirstFillBlankModel getFirstFillBlankModel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		FirstFillBlankModel firstFillBlankModel = new FirstFillBlankModel();
		firstFillBlankModel.setUserId(userBo.getUser().getId());
		firstFillBlankModel.setItem0101(isNotbank(request.getParameter("item0101"))?Integer.valueOf(request.getParameter("item0101")):null);
		firstFillBlankModel.setItem0102(isNotbank(request.getParameter("item0102"))?Integer.valueOf(request.getParameter("item0102")):null);
		firstFillBlankModel.setItem0103(isNotbank(request.getParameter("item0103"))?Integer.valueOf(request.getParameter("item0103")):null);
		firstFillBlankModel.setItem0104(isNotbank(request.getParameter("item0104"))?Integer.valueOf(request.getParameter("item0104")):null);
		firstFillBlankModel.setItem0201(isNotbank(request.getParameter("item0201"))?Integer.valueOf(request.getParameter("item0201")):null);
		firstFillBlankModel.setItem0202(isNotbank(request.getParameter("item0202"))?Float.valueOf(request.getParameter("item0202")):null);
		firstFillBlankModel.setItem0203(isNotbank(request.getParameter("item0203"))?Integer.valueOf(request.getParameter("item0203")):null);
		firstFillBlankModel.setItem0204(isNotbank(request.getParameter("item0204"))?Float.valueOf(request.getParameter("item0204")):null);
		firstFillBlankModel.setItem0205(isNotbank(request.getParameter("item0205"))?Float.valueOf(request.getParameter("item0205")):null);
		firstFillBlankModel.setItem0206(isNotbank(request.getParameter("item0206"))?Float.valueOf(request.getParameter("item0206")):null);
		firstFillBlankModel.setItem0207(isNotbank(request.getParameter("item0207"))?Integer.valueOf(request.getParameter("item0207")):null);
		firstFillBlankModel.setItem0208(isNotbank(request.getParameter("item0208"))?Integer.valueOf(request.getParameter("item0208")):null);
		firstFillBlankModel.setItem0301(isNotbank(request.getParameter("item0301"))?Float.valueOf(request.getParameter("item0301")):null);
		firstFillBlankModel.setItem0302(isNotbank(request.getParameter("item0302"))?Float.valueOf(request.getParameter("item0302")):null);
		firstFillBlankModel.setItem0401(isNotbank(request.getParameter("item0401"))?Float.valueOf(request.getParameter("item0401")):null);
		firstFillBlankModel.setItem0402(isNotbank(request.getParameter("item0402"))?Float.valueOf(request.getParameter("item0402")):null);
		firstFillBlankModel.setItem0501(isNotbank(request.getParameter("item0501"))?Integer.valueOf(request.getParameter("item0501")):null);
		firstFillBlankModel.setItem0502(isNotbank(request.getParameter("item0502"))?Integer.valueOf(request.getParameter("item0502")):null);
		firstFillBlankModel.setItem0503(isNotbank(request.getParameter("item0503"))?Integer.valueOf(request.getParameter("item0503")):null);
		firstFillBlankModel.setItem0601(isNotbank(request.getParameter("item0601"))?Integer.valueOf(request.getParameter("item0601")):null);
		firstFillBlankModel.setItem0602(isNotbank(request.getParameter("item0602"))?Integer.valueOf(request.getParameter("item0602")):null);
		firstFillBlankModel.setItem0701(isNotbank(request.getParameter("item0701"))?Integer.valueOf(request.getParameter("item0701")):null);
		firstFillBlankModel.setItem0702(isNotbank(request.getParameter("item0702"))?Integer.valueOf(request.getParameter("item0702")):null);
		firstFillBlankModel.setItem0703(isNotbank(request.getParameter("item0703"))?Integer.valueOf(request.getParameter("item0703")):null);
		firstFillBlankModel.setItem0704(isNotbank(request.getParameter("item0704"))?Integer.valueOf(request.getParameter("item0704")):null);
		firstFillBlankModel.setItem0705(isNotbank(request.getParameter("item0705"))?Integer.valueOf(request.getParameter("item0705")):null);
		firstFillBlankModel.setItem0706(isNotbank(request.getParameter("item0706"))?Integer.valueOf(request.getParameter("item0706")):null);
		firstFillBlankModel.setItem0707(isNotbank(request.getParameter("item0707"))?Integer.valueOf(request.getParameter("item0707")):null);
		firstFillBlankModel.setItem0708(isNotbank(request.getParameter("item0708"))?Integer.valueOf(request.getParameter("item0708")):null);
		firstFillBlankModel.setItem0709(isNotbank(request.getParameter("item0709"))?Integer.valueOf(request.getParameter("item0709")):null);
		firstFillBlankModel.setItem0710(isNotbank(request.getParameter("item0710"))?Integer.valueOf(request.getParameter("item0710")):null);
		firstFillBlankModel.setItem0801(isNotbank(request.getParameter("item0801"))?Integer.valueOf(request.getParameter("item0801")):null);
		firstFillBlankModel.setItem0802(isNotbank(request.getParameter("item0802"))?Integer.valueOf(request.getParameter("item0802")):null);
		firstFillBlankModel.setItem0803(isNotbank(request.getParameter("item0803"))?Integer.valueOf(request.getParameter("item0803")):null);
		firstFillBlankModel.setItem0804(isNotbank(request.getParameter("item0804"))?Integer.valueOf(request.getParameter("item0804")):null);
		firstFillBlankModel.setItem0805(isNotbank(request.getParameter("item0805"))?Integer.valueOf(request.getParameter("item0805")):null);
		firstFillBlankModel.setItem0806(isNotbank(request.getParameter("item0806"))?Float.valueOf(request.getParameter("item0806")):null);
		firstFillBlankModel.setItem0901(isNotbank(request.getParameter("item0901"))?Integer.valueOf(request.getParameter("item0901")):null );
		firstFillBlankModel.setItem0902(isNotbank(request.getParameter("item0902"))?Integer.valueOf(request.getParameter("item0902")):null);
		firstFillBlankModel.setItem0903(isNotbank(request.getParameter("item0903"))?Integer.valueOf(request.getParameter("item0903")):null);
		firstFillBlankModel.setItem0904(isNotbank(request.getParameter("item0904"))?request.getParameter("item0904"):null);
		return firstFillBlankModel;
	}
	
	private boolean isNotbank(String temp){
		if(temp!=null&&!("").equals(temp))
			return true;
		else 
			return false;
	}
	
	private boolean arrayIsNotBank(String[] strs) {
		
		if (strs!=null&&strs.length>0)
			return true;
		else
			return false;
	}
	public void init() throws ServletException {
		// Put your code here
	}

}
